package method;

import java.util.Scanner;

/**
 * @author 孤雪唯方
 * @date 2025/05/09
 * @description 求阶乘的和
 */
public class FactorialSum {
    /**
     * 求一个整数的阶乘
     *
     * @param n 一个整数
     * @return int
     */
    public static int factorial(int n) {
        System.out.println("计算"+ n + "的阶乘中……");
        int result = 1;
        for (int i = 1; i <n+1; i++) {
            result *= i;
        }
        System.out.println(n + "! = " + result);
        return result;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个需要求和的阶乘的上界整数：");// 输入上界,即输入5，则求1!+2!+3!+4!+5!
        int n = scanner.nextInt();
        int sum = 0;// 阶乘和
        for (int i = 1; i < n+1; i++) {
            sum += factorial(i);
        }
        System.out.println("阶乘和为：" + sum);

    }
}
